<template>
  <div class="uploade_main">
    <div class="useImage">
      <div class="bigImage">
        <img :src="photoUrl" v-if="photoUrl && !fileList.url" />
        <img :src="imgBaseUrl + fileList.url" v-else-if="fileList.url && photoUrl" />
      </div>
      <div class="smallImage">
        <img class="img" :src="photoUrl" v-if="photoUrl && !fileList.url" />
        <img class="img" :src="imgBaseUrl + fileList.url" v-else-if="fileList.url && photoUrl" />
        <el-upload
          class="upload-demo"
          action="https://bootapi.51bmj.cn/api/home/upload"
          :data="{ biz: 'pc_web' }"
          :on-success="handleSuccess"
        >
          <div class="usertn">
            <img src="../../../../static/image/userInfo/uploadImg.png" alt />重新选择
          </div>
        </el-upload>
      </div>
    </div>
    <button class="changeHanld" @click="uploading" :class="fileList.url != '' ? 'active' : ''">上传</button>
  </div>
</template>

<script>
import { postRequest, imgBaseUrl } from "../../../api/api.js";
export default {
  data() {
    return {
      isLoading: false, // 点击上传按钮
      fileList: {
        name: "",
        url: "",
        imgBaseUrl: ""
      }
    };
  },
  props: {
    photoUrl: {
      type: String,
      default: ""
    }
  },
  methods: {
    uploading() {
      // 点击上传按钮
      let fileUrl = this.fileList.url;
      postRequest(
        "/api/ums/umsUsers/updateHeadPortrait?newHeadPortrait=" + fileUrl,
        {}
      ).then(res => {
        console.log(fileUrl);
        if (res.code == 200) {
          this.$emit("refreshAvatar", fileUrl);
          this.$message({
            type: "success",
            message: "修改成功"
          });
        } else {
          this.$message({
            type: "success",
            message: "修改失败"
          });
        }
      });
      this.$emit("isloading", this.isLoading);
      this.fileList.url = "";
    },

    handleSuccess(response) {
      this.fileList.url = "";
      this.fileList.url = response.result;
    }
  },
  mounted() {
    this.imgBaseUrl = imgBaseUrl;
  }
};
</script>

<style lang="less" scoped>
.uploade_main {
  display: flex;
  flex-direction: column;
  .useImage {
    display: flex;
    height: 150px;
    justify-content: space-between;
    .bigImage {
      border-radius: 50%;
      width: 150px !important;
      img {
        width: 100%;
        height: 100%;
        border-radius: 50%;
      }
    }
    .smallImage {
      width: 110px;
      height: 60px;
      .img {
        border-radius: 50%;
        height: 60px;
        width: 60px !important;
        margin-left: 50px;
      }
      .upload-demo {
        margin-top: 10px;
        .usertn {
          display: flex;
          align-items: center;
          justify-content: center;
          width: 120px;
          font-size: 14px;
          height: 40px;
          border: 1px solid #ccc;
          img {
            height: 18px;
            margin-right: 10px;
          }
        }
      }
    }
  }

  button {
    width: 100%;
    height: 40px;
    background-color: #ccc;
    margin-top: 20px;
    border-radius: 6px;
    font-size: 16px;
    color: #fff;
    &.active {
      background-color: #409eff;
      color: #fff;
    }
  }
}
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
